<?php require_once dirname(__FILE__) . "/shared/config/config.php"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />

        <title>Index - Tesdo</title>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php'; ?>
        <link href="<?php echo LINK_ROOT . DIR_LAYOUT_CSS; ?>/publication.css" rel="stylesheet" />
        <meta name="viewport" content="width=device-width" />
        <?php
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_journal.php";
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . "lib_pager.php";

        $journal_access = new dao_journal();

        $title_search = "";
        if (!empty($_GET['search'])) {
            $title_search = $_GET['search'];
            $total_list = $journal_access->search_with_title($title_search);
        } else if (!empty($_GET['letter_search'])) {
            $letter_search = $_GET['letter_search'];
            $total_list = $journal_access->search_with_first_letter($letter_search);
        } else {
            $total_list = $journal_access->get_all();
        }


        if (!empty($_GET['p'])) {
            $current_page = $_GET['p'];
        } else {
            $current_page = 1;
        }
        $page_size = 5;

        $paging = new lib_pager();
        $total_page = $paging->get_total_page($total_list, $page_size);
        $journals = $paging->get_current_page_list($page_size, $current_page, $total_list);
        ?>
    </head>
    <body>
        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php'; ?>

        <!-- Body -->
        <div id="body">
            <section class="content-wrapper main-content clear-fix">
                <div class="container-col-w-sidebar">
                </div>
                <div class="clear"></div>
                <div class="container-col-w-sidebar">
                    <!-- Content -->
                    <div class="command-box">
                        <form method="GET" action="?">
                            <input name="search" type="text" class="command-box-form" value="<?php echo $title_search; ?>" placeholder="Enter titles of journal...">
                            <span class="command-box-icon">
                                <span class="command-box-action"><img src="<?php echo LINK_ROOT . DIR_LAYOUT_IMAGES . "search.png" ?>" /></span>
                            </span>
                        </form>
                    </div>
                    <table>
                        <tbody>
                            <tr>
                                <?php foreach (range('A', 'Z') as $char) { ?>
                                    <td class="letter-search">
                                        <span>
                                            <a href="?letter_search=<?php echo $char; ?>" ><?php echo $char; ?></a>
                                        </span>
                                    </td>
                                <?php } ?>
                            </tr>
                        </tbody>
                    </table>
                    <div id="post-602" class="post-602 page type-page status-publish hentry">
                        <?php
                        foreach ($journals as $i => $item) {
                            ?>
                            <h5>
                                <a href="journal_detail.php?id=<?php echo $item['JournalID']; ?>" class="hr" onmouseover="none" style="color: #335EBE;">
                                    <?php echo $item['Title']; ?>
                                </a>
                            </h5>
                            <p style="text-align: justify;">
                                <a target="_blank" href="journal_detail.php?id=<?php echo $item['JournalID']; ?>" title="<?php echo $item['Title'] ?>">
                                    <img width="88" height="123" src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_JOURNAL_IMAGES . $item['Photo']; ?>" alt="journals" class="alignleft size-full wp-image-844">
                                </a>
                            <p style="margin-top: -35px"><?php echo $item['ShortDescription']; ?></p>
                            <p style="text-align: left;">
                                <?php if (!empty($item['PrintISSN'])) { ?>
                                    ISSN: <?php echo $item['PrintISSN']; ?> (print)<br>
                                <?php } ?>
                                <?php if (!empty($item['OnlineISSN'])) { ?>
                                    ISSN: <?php echo $item['OnlineISSN']; ?> (online)<br>
                                <?php } ?>
                                <?php if (!empty($item['ImpactFactor'])) { ?>
                                    <strong>Impact Factor: <?php echo $item['ImpactFactor']; ?></strong>
                                <?php } ?>
                            </p>
                            <div class="clear"></div>
                            <?php
                        }
                        ?>
                    </div>
                    <!-- /Content -->
                </div>
                <table width="100%">
                    <tbody>
                        <tr>
                            <td id="MainContent_paging" align="center">
                                <ul>
                                    <?php
                                    for ($i = 1; $i <= $total_page; $i++) {
                                        ?>
                                        <li><a href="?p=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                                    <?php }
                                    ?>
                                </ul>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <br clear="All" />
                <br clear="All" />
            </section>
        </div>
        <!-- End Body -->

        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php'; ?>
    </body>
</html>